import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { HttpResponse } from '../interfaces/base.interface';

@Injectable()
export class TransformInterceptor<T>
  implements NestInterceptor<T, HttpResponse<T>>
{
  intercept(
    context: ExecutionContext,
    next: CallHandler
  ): Observable<HttpResponse<T>> {
    return next.handle().pipe(
      map(data => ({
        code: 0, // 0 表示成功
        message: 'Success',
        data,
      }))
    );
  }
}
